﻿using car.Controllers;
using car.Models;
using Microsoft.AspNetCore.Mvc;
using System;

namespace VMS.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return View("login");
        }
        [HttpPost]
        public IActionResult Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                ViewBag.notice = "用户名或密码为空";
            }
            else
            {
                using var ctx = new carContext();
                var admin = ctx.Admin.Find(username);
                if (admin != null)
            {
                    if (admin.Apwd == password)
                        return RedirectToAction("AdminIndex", "Home");
                    else
                        ViewBag.notice = "密码错误";
            }
                else
            {
                    var user = ctx.User.Find(username);
                    if (user != null)
                    {
                        if (user.Upwd == password)
                        {
                            HomeController.UserUid = user.Uid;
                            return RedirectToAction("UserIndex", "User");
                        }
                        else
                            ViewBag.notice = "密码错误";
            }
            else
            {
                        ViewBag.notice = "没有此用户";
                    }
                }    
            }
            return View();
        } 
    }
}
